package com.eliza.base.views

import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.core.EaseIn
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.eliza.base.R
import com.eliza.base.viewrouter.NavDataBaseEnum
import com.eliza.base.viewrouter.NavDeviceEnum
import com.eliza.base.viewrouter.NavLearnEnum
import com.eliza.base.viewrouter.NavRootScreenEnum
import com.eliza.base.views.component.NavComponentEnum
import com.eliza.library.ui.inter.IBaseRoute


//========================================================


@Composable
@Preview
fun NavHostMainPre() {
    NavHostMain()
}

@Composable
fun NavHostMain(
    modifier: Modifier = Modifier,
    navController: NavHostController = rememberNavController(),
    entries: List<IBaseRoute> = NavRootScreenEnum.entries.toList(),
) {
    val bottomHeight = 60.dp
    Scaffold(
        topBar = { TopAppBarItem(navController) },
        bottomBar = {
            NavigationBar(
                modifier = Modifier.height(bottomHeight),
                containerColor = NavigationBarDefaults.containerColor
            ) {
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                val currentDestination = navBackStackEntry?.destination
//                val textStyle = TextStyle(color = Color.Black, fontSize = 2.em)

                entries.forEach { screen ->
                    IconButton(
                        modifier = Modifier
                            .weight(
                                1 / entries.size.toFloat(),
                                true
                            ),
                        onClick = {
                            navController.navigate(screen.title) {
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = true
                                }
                                launchSingleTop = true
                                restoreState = true
                            }
                        },
                    ) {
                        Icon(
                            screen.imageVector,
                            contentDescription = null,
                            modifier = Modifier
                                .size(bottomHeight / 2)
                                .background(
                                    if (currentDestination?.hierarchy?.any { it.route == screen.title } == true) Color.Gray else Color.Transparent
                                ),
                        )

                    }

                }
            }
        }
    ) { paddingValues ->
        NavHost(
            navController,
            startDestination = entries[0].title,
            Modifier.padding(paddingValues),
            enterTransition = { EnterTransition.None },
            exitTransition = { ExitTransition.None }
        ) {
            // 第一层
            entries.forEach { route ->
                composable(
                    route.title,
                    enterTransition = {
                        fadeIn(
                            animationSpec = tween(
                                300, easing = LinearEasing
                            )
                        )
                    },
                ) {
                    route.content(navController)
                }
            }
            //第二层
            NavDataBaseEnum.RegisterRoute( navController,this)
            NavDeviceEnum.RegisterRoute( navController,this)
            NavLearnEnum.RegisterRoute( navController,this)
            NavComponentEnum.RegisterRoute( navController,this)
            //地三层
        }
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopAppBarItem(navController: NavHostController) {
    TopAppBar(title = {
        Text(text = stringResource(id = R.string.arrow_back))
    }, navigationIcon = {
        IconButton(onClick = { navController.navigateUp() }) {
            Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = null)
        }
    }, actions = {
        IconButton(onClick = { /**/ }) {
            Icon(Icons.Filled.Share, contentDescription = null)
        }
        IconButton(onClick = { /**/ }) {
            Icon(Icons.Filled.Settings, contentDescription = null)
        }
    }
    )
}

